package edu.faytechcc.csc.gui.search;

import com.sun.rowset.CachedRowSetImpl;
import edu.faytechcc.csc.gui.search.details.DetailsFrame;
import edu.faytechcc.csc.messages.NetworkConnection;
import java.awt.BorderLayout;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * Creates a multiple panels to display information to the user, and sets 
 * class level variables to be retrieved by other classes.
 *
 * @author Stephanie Mckenzie and Jasmin Pastari
 */
public class SearchFrame extends JFrame
{

    private SearchPanel searchPanel;
    private ListPanel listPanel;
    private TopFivePanel topFivePanel;
    private String engTitle;
    private String japTitle;
    private String description;
    private String numVol;
    private String genera;
    private String artist; //or company in the anime table
    private String photo = "";
    private NetworkConnection conn;
    private boolean random = false;

    /**
     * Constructor that accepts a String and creates a SearchPanel, ListPanel
     * and TopFivePanel
     *
     * @param ip
     * @throws IOException
     */
    public SearchFrame(String ip)
            throws IOException
    {
        conn = null;
        // Set the Window title
        this.setTitle("Anime Reccomendation Generator");
        // Close the Window when X is clicked
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Creates a null object for the socket
        Socket socket = null;
        // Sets the serverIP string to the ip used in the constructor
        final String SERVER_IP = ip;
        
        try
        {
            socket = new Socket(SERVER_IP,
                    4444);
            System.out.println("Server connected: "
                    + socket.getInetAddress());
            System.out.println("+++++++++++++\n");
            // Sets up the connection between the clients and the server
            conn = new NetworkConnection(socket);
            conn.connectClient();
            conn.addSearchPanel(this);
        } catch (UnknownHostException uhe)
        {
            System.err.println();
            System.exit(36);
        }
        // make the panels
        this.setLayout(new BorderLayout());
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        searchPanel = new SearchPanel(conn);
        listPanel = new ListPanel(this);
        topFivePanel = new TopFivePanel(this);
        //add the menu
        CreateARGMenuBar menuBar = new CreateARGMenuBar(searchPanel,
                listPanel);
        this.setJMenuBar(menuBar.getFileMenu());
        //add the panels
        this.add(mainPanel,
                BorderLayout.CENTER);
        mainPanel.add(searchPanel,
                BorderLayout.NORTH);
        mainPanel.add(listPanel,
                BorderLayout.WEST);
        mainPanel.add(topFivePanel,
                BorderLayout.EAST);

        //Display the window.
        this.pack();
        this.setVisible(true);

    }

    /**
     * Updates the frames values
     *
     * @param results
     */
    public void updateTable(Object results)
    {
        CachedRowSetImpl res = (CachedRowSetImpl) results;
        try
        {
            res.next();
            engTitle = res.getString(2);
            japTitle = res.getString(3);
            description = res.getString(4);
            numVol = res.getString(5);
            genera = res.getString(6);
            artist = res.getString(7);
            photo = "ARGDB\\" + res.getString(8);
            DetailsFrame details = new DetailsFrame(engTitle, this);

        } catch (Exception ex)
        {
            try
            {
                res.next();
                engTitle = res.getString("ENG_TITLE");
                photo = "ARGDB\\" + res.getString("PHOTO");
                topFivePanel.createButton(engTitle, photo);
            } catch (SQLException ex1)
            {
                this.listPanel.setTable(res);
            }
        }
    }

    /**
     * @return the engTitle
     */
    public String getEngTitle()
    {
        return engTitle;
    }

    /**
     * @return the japTitle
     */
    public String getJapTitle()
    {
        return japTitle;
    }

    /**
     * @return the description
     */
    public String getDescription()
    {
        return description;
    }

    /**
     * @return the numVol
     */
    public String getNumVol()
    {
        return numVol;
    }

    /**
     * @return the genera
     */
    public String getGenera()
    {
        return genera;
    }

    /**
     * @return the artist
     */
    public String getArtist()
    {
        return artist;
    }

    /**
     * @return the photo
     */
    public String getPhoto()
    {
        return photo;
    }

    /**
     * @return the conn
     */
    public NetworkConnection getConn()
    {
        return conn;
    }

    /**
     * @return the random
     */
    public boolean isRandom()
    {
        return random;
    }

    /**
     * @param random the random to set
     */
    public void setRandom(boolean random)
    {
        this.random = random;
    }
}
